#include <iostream>
#include <string>
using namespace std;
// for access()
#include <io.h>
// for _getcwd()
#include <direct.h>
#include <string.h>

//*DEBUG&&
#define OUT(x) cout << #x << " = " << x << endl

#ifdef __UNDERC__
// not always defined??
void strupr(char* str)
{
 int incr = (int)'A' - (int)'a';
 for(; *str; str++) {
  char p = *str;
  if (p >= 'a' && p <= 'z') *str += incr;
 }
}
#endif

bool is_one_of(string ext, char** table)
{
 const char *p_ext = ext.c_str();
 while (*table && strcmp(p_ext,*table) != 0) table++;
 return *table != NULL;
}

bool file_exists(string file)
{
 return _access(file.c_str(),0) != -1;
}

string append_extensions_until_found(string fname, char** exts)
{
 while (*exts) {
   string file = fname + *exts;
   if (file_exists(file)) return file;
   exts++;
 }
 return "";
}

string get_extension(string path)
{
  const char* endp = strrchr(path.c_str(),'.');
  if (endp) {
     char ext[27];
     strcpy(ext,endp);
     strupr(ext);
     return ext;
  } else return "";
}

string get_filename(string path)
{
  int pos = path.rfind('.');
  if (pos != string::npos) {
     return path.substr(0,pos);
  } else return path;
}

string get_path(string path)
{
  int pos = path.rfind('\\');  // Win32 alert
  if (pos != string::npos) {
     return path.substr(0,pos);
  } else return "";
}

string get_cwd()
{
 char buff[_MAX_PATH];
 return _getcwd(buff,_MAX_PATH);
}

string canonical_form(string _curr_dir, string _file_path)
{
// check whether this is an absolute path
 if (_file_path[1] == ':' || _file_path[0] == '\\') return _file_path;
 else return _curr_dir + _file_path;
}





 
